package com.hamster.crmpre.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamster.crmpre.mapper.ProjectCourseMapper;
import com.hamster.crmpre.pojo.ProjectCourse;
import com.hamster.crmpre.service.ProjectCourseService;
import com.hamster.crmpre.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Tarry
 * @create 2019/12/24 13:57
 */
@Service
public class ProjectCourseServiceImpl implements ProjectCourseService {
    @Autowired
    private ProjectCourseMapper projectCourseMapper;

    /**
     * 新增项目课程
     * @param projectCourse
     */
    @Override
    public void insert(ProjectCourse projectCourse) {
        projectCourse.setDel("0");
        projectCourse.setInputUser("功能的待完善");
        projectCourse.setInputTime(new Date());
        projectCourseMapper.insert(projectCourse);
    }

    /**
     * 删除项目课程
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<String> ids) {
        for (String id : ids) {
            projectCourseMapper.deleteTag(id);
        }
    }

    /**
     * 获取项目课程详情
     * @param id
     * @return
     */
    @Override
    public ProjectCourse get(String id) {
        return projectCourseMapper.selectById(id);
    }

    /**
     * 修改项目课程
     * @param projectCourse
     */
    @Override
    public void update(ProjectCourse projectCourse) {
        projectCourse.setInputTime(new Date());
        projectCourse.setInputUser("功能待完善");
        projectCourseMapper.updateById(projectCourse);
    }

    /**
     * 分页条件查询项目课程
     * @param pageSize
     * @param currentPage
     * @param title
     * @return
     */
    @Override
    public PageResult<ProjectCourse> listProjectCourses(Integer pageSize, Integer currentPage, String title) {
        //分页
        Page<ProjectCourse> page = new Page<>(currentPage, pageSize);

        QueryWrapper<ProjectCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0");
        if (StringUtils.isNotBlank(title)){
            queryWrapper.like("title","%"+title+"%");
        }
        IPage pageList = projectCourseMapper.selectPage(page, queryWrapper);
        List<ProjectCourse> currentList =(List<ProjectCourse>) pageList.getRecords();
        Long pages = pageList.getPages();//总页数
        return new PageResult<ProjectCourse>(pageList.getTotal(),pages.intValue(),currentList);
    }

    /**
     * 下拉框获取项目课程列表
     * @return
     */
    @Override
    public List<ProjectCourse> listOptions() {
        QueryWrapper<ProjectCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0");
        return projectCourseMapper.selectList(queryWrapper);

    }

    /**
     * 根据课程id集合，查询课程【列表】
     * @param courseList
     * @return
     */
    @Override
    public List<ProjectCourse> queryCourse(List<String> courseList){
        return projectCourseMapper.selectBatchIds(courseList);
    }



}
